{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始尝试GBDT+LR 的模型融合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda2\\lib\\site-packages\\sklearn\\cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.\n",
      "  \"This module will be removed in 0.20.\", DeprecationWarning)\n",
      "C:\\ProgramData\\Anaconda2\\lib\\site-packages\\sklearn\\grid_search.py:42: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. This module will be removed in 0.20.\n",
      "  DeprecationWarning)\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.ensemble import GradientBoostingClassifier #gbdt\n",
    "from sklearn import cross_validation, metrics\n",
    "from sklearn.grid_search import GridSearchCV\n",
    "\n",
    "import matplotlib.pylab as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Unnamed: 0</th>\n",
       "      <th>id</th>\n",
       "      <th>click</th>\n",
       "      <th>C1</th>\n",
       "      <th>banner_pos</th>\n",
       "      <th>device_type</th>\n",
       "      <th>device_conn_type</th>\n",
       "      <th>C14</th>\n",
       "      <th>C15</th>\n",
       "      <th>C16</th>\n",
       "      <th>...</th>\n",
       "      <th>top_30_device_ip</th>\n",
       "      <th>top_10_device_model</th>\n",
       "      <th>top_25_device_model</th>\n",
       "      <th>top_5_device_model</th>\n",
       "      <th>top_50_device_model</th>\n",
       "      <th>top_1_device_model</th>\n",
       "      <th>top_2_device_model</th>\n",
       "      <th>top_15_device_model</th>\n",
       "      <th>top_20_device_model</th>\n",
       "      <th>top_30_device_model</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>186848</td>\n",
       "      <td>1.819859e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>20633</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>815809</td>\n",
       "      <td>3.357563e+18</td>\n",
       "      <td>0</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>15706</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>51654</td>\n",
       "      <td>1.719468e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>21611</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>979514</td>\n",
       "      <td>1.349057e+19</td>\n",
       "      <td>0</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>19251</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>58702</td>\n",
       "      <td>1.817488e+19</td>\n",
       "      <td>1</td>\n",
       "      <td>1005</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>15704</td>\n",
       "      <td>320</td>\n",
       "      <td>50</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 120 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   Unnamed: 0            id  click    C1  banner_pos  device_type  \\\n",
       "0      186848  1.819859e+19      0  1005           0            1   \n",
       "1      815809  3.357563e+18      0  1005           0            1   \n",
       "2       51654  1.719468e+19      0  1005           0            1   \n",
       "3      979514  1.349057e+19      0  1005           0            1   \n",
       "4       58702  1.817488e+19      1  1005           0            1   \n",
       "\n",
       "   device_conn_type    C14  C15  C16         ...           top_30_device_ip  \\\n",
       "0                 0  20633  320   50         ...                          1   \n",
       "1                 0  15706  320   50         ...                          1   \n",
       "2                 0  21611  320   50         ...                          1   \n",
       "3                 0  19251  320   50         ...                          1   \n",
       "4                 0  15704  320   50         ...                          1   \n",
       "\n",
       "   top_10_device_model  top_25_device_model  top_5_device_model  \\\n",
       "0                    0                    0                   0   \n",
       "1                    1                    1                   1   \n",
       "2                    1                    1                   1   \n",
       "3                    1                    1                   1   \n",
       "4                    1                    1                   1   \n",
       "\n",
       "   top_50_device_model  top_1_device_model  top_2_device_model  \\\n",
       "0                    1                   0                   0   \n",
       "1                    1                   1                   1   \n",
       "2                    1                   1                   1   \n",
       "3                    1                   0                   0   \n",
       "4                    1                   1                   1   \n",
       "\n",
       "   top_15_device_model  top_20_device_model  top_30_device_model  \n",
       "0                    0                    0                    0  \n",
       "1                    1                    1                    1  \n",
       "2                    1                    1                    1  \n",
       "3                    1                    1                    1  \n",
       "4                    1                    1                    1  \n",
       "\n",
       "[5 rows x 120 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#读取数据\n",
    "train = pd.read_csv('./train_FE.csv')\n",
    "train.head(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 取出X和y\n",
    "X=train.drop(['click'],axis=1)\n",
    "y = train['click']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 不管任何参数 先试试GBDT模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Accuracy : 0.8412\n",
      "AUC Score (Train): 0.727429\n"
     ]
    }
   ],
   "source": [
    "gbm0 = GradientBoostingClassifier()\n",
    "gbm0.fit(X,y)\n",
    "y_pred = gbm0.predict(X)\n",
    "y_predprob = gbm0.predict_proba(X)[:,1]\n",
    "print \"Accuracy : %.4g\" % metrics.accuracy_score(y.values, y_pred)\n",
    "print \"AUC Score (Train): %f\" % metrics.roc_auc_score(y, y_predprob)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 可以看出准确率还可以"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 先使用GBDT 进行训练先找到最佳参数之后再进行模型融合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 首先我们从步长(learning rate)和迭代次数(n_estimators)入手。\n",
    "一般来说,开始选择一个较小的步长来网格搜索最好的迭代次数。这里，我们将步长初始值设置为0.1。对于迭代次数进行网格搜索如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([mean: 0.71874, std: 0.00435, params: {'n_estimators': 10},\n",
       "  mean: 0.72586, std: 0.00563, params: {'n_estimators': 20},\n",
       "  mean: 0.72846, std: 0.00546, params: {'n_estimators': 30},\n",
       "  mean: 0.73056, std: 0.00525, params: {'n_estimators': 40},\n",
       "  mean: 0.73189, std: 0.00510, params: {'n_estimators': 50},\n",
       "  mean: 0.73289, std: 0.00542, params: {'n_estimators': 60},\n",
       "  mean: 0.73334, std: 0.00561, params: {'n_estimators': 70},\n",
       "  mean: 0.73394, std: 0.00547, params: {'n_estimators': 80},\n",
       "  mean: 0.73439, std: 0.00531, params: {'n_estimators': 90}],\n",
       " {'n_estimators': 90},\n",
       " 0.7343881033644699)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "param_test1 = {'n_estimators':range(10,100,10)} #数的数目 10--100 步长为10\n",
    "gsearch1 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.1, min_samples_split=300,\n",
    "                                  min_samples_leaf=20,max_depth=8,max_features='sqrt', subsample=0.8,random_state=10), \n",
    "                       param_grid = param_test1, scoring='roc_auc',iid=False,cv=5)\n",
    "gsearch1.fit(X,y)\n",
    "gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 90才是最优 所以 我们继续进行调优"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([mean: 0.73439, std: 0.00531, params: {'n_estimators': 90},\n",
       "  mean: 0.73446, std: 0.00532, params: {'n_estimators': 100},\n",
       "  mean: 0.73475, std: 0.00532, params: {'n_estimators': 110},\n",
       "  mean: 0.73512, std: 0.00528, params: {'n_estimators': 120},\n",
       "  mean: 0.73513, std: 0.00520, params: {'n_estimators': 130},\n",
       "  mean: 0.73517, std: 0.00519, params: {'n_estimators': 140},\n",
       "  mean: 0.73519, std: 0.00513, params: {'n_estimators': 150},\n",
       "  mean: 0.73525, std: 0.00505, params: {'n_estimators': 160},\n",
       "  mean: 0.73543, std: 0.00490, params: {'n_estimators': 170},\n",
       "  mean: 0.73531, std: 0.00496, params: {'n_estimators': 180},\n",
       "  mean: 0.73537, std: 0.00501, params: {'n_estimators': 190},\n",
       "  mean: 0.73522, std: 0.00509, params: {'n_estimators': 200}],\n",
       " {'n_estimators': 170},\n",
       " 0.7354265035959298)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "param_test2 = {'n_estimators':range(90,201,10)} #\n",
    "gsearch2 = GridSearchCV(estimator = GradientBoostingClassifier(learning_rate=0.1, min_samples_split=300,\n",
    "                                  min_samples_leaf=20,max_depth=8,max_features='sqrt', subsample=0.8,random_state=10), \n",
    "                       param_grid = param_test2, scoring='roc_auc',iid=False,cv=5)\n",
    "gsearch2.fit(X,y)\n",
    "gsearch2.grid_scores_, gsearch2.best_params_, gsearch2.best_score_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "n_estimators = 170 找到了一个合适的迭代次数，现在我们开始对决策树进行调参。首先我们对决策树最大深度max_depth和内部节点再划分所需最小样本数min_samples_split进行网格搜索。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "([mean: 0.71861, std: 0.00522, params: {'min_samples_split': 100, 'max_depth': 3},\n",
       "  mean: 0.71858, std: 0.00527, params: {'min_samples_split': 300, 'max_depth': 3},\n",
       "  mean: 0.71836, std: 0.00548, params: {'min_samples_split': 500, 'max_depth': 3},\n",
       "  mean: 0.71788, std: 0.00540, params: {'min_samples_split': 700, 'max_depth': 3},\n",
       "  mean: 0.73067, std: 0.00566, params: {'min_samples_split': 100, 'max_depth': 5},\n",
       "  mean: 0.73040, std: 0.00562, params: {'min_samples_split': 300, 'max_depth': 5},\n",
       "  mean: 0.73077, std: 0.00569, params: {'min_samples_split': 500, 'max_depth': 5},\n",
       "  mean: 0.72928, std: 0.00584, params: {'min_samples_split': 700, 'max_depth': 5},\n",
       "  mean: 0.73418, std: 0.00525, params: {'min_samples_split': 100, 'max_depth': 7},\n",
       "  mean: 0.73451, std: 0.00526, params: {'min_samples_split': 300, 'max_depth': 7},\n",
       "  mean: 0.73435, std: 0.00490, params: {'min_samples_split': 500, 'max_depth': 7},\n",
       "  mean: 0.73474, std: 0.00546, params: {'min_samples_split': 700, 'max_depth': 7},\n",
       "  mean: 0.73386, std: 0.00481, params: {'min_samples_split': 100, 'max_depth': 9},\n",
       "  mean: 0.73491, std: 0.00461, params: {'min_samples_split': 300, 'max_depth': 9},\n",
       "  mean: 0.73503, std: 0.00439, params: {'min_samples_split': 500, 'max_depth': 9},\n",
       "  mean: 0.73601, std: 0.00525, params: {'min_samples_split': 700, 'max_depth': 9},\n",
       "  mean: 0.73055, std: 0.00432, params: {'min_samples_split': 100, 'max_depth': 11},\n",
       "  mean: 0.73390, std: 0.00461, params: {'min_samples_split': 300, 'max_depth': 11},\n",
       "  mean: 0.73491, std: 0.00461, params: {'min_samples_split': 500, 'max_depth': 11},\n",
       "  mean: 0.73527, std: 0.00399, params: {'min_samples_split': 700, 'max_depth': 11},\n",
       "  mean: 0.72655, std: 0.00423, params: {'min_samples_split': 100, 'max_depth': 13},\n",
       "  mean: 0.73118, std: 0.00475, params: {'min_samples_split': 300, 'max_depth': 13},\n",
       "  mean: 0.73342, std: 0.00413, params: {'min_samples_split': 500, 'max_depth': 13},\n",
       "  mean: 0.73464, std: 0.00434, params: {'min_samples_split': 700, 'max_depth': 13}],\n",
       " {'max_depth': 9, 'min_samples_split': 700},\n",
       " 0.7360073136519084)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "param_test3 = {'max_depth':range(3,14,2), 'min_samples_split':range(100,801,200)}\n",
    "gsearch3 = GridSearchCV(estimator = GradientBoostingClassifier(n_estimators=170,learning_rate=0.1,\n",
    "                                                               min_samples_leaf=20,\n",
    "                                                               max_features='sqrt',\n",
    "                                                               subsample=0.8\n",
    "                                                               ,random_state=10), \n",
    "                       param_grid = param_test3, scoring='roc_auc',iid=False,cv=5)\n",
    "gsearch3.fit(X,y)\n",
    "gsearch3.grid_scores_, gsearch3.best_params_, gsearch3.best_score_"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "由于使用GBDT 做特征选择的话并不需要太多的对GBDT调参 所以下面就开始进行GBDT +LR 的训练了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 开始进行模型融合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 弱分类器的数目\n",
    "\n",
    "from sklearn.cross_validation import train_test_split\n",
    "# 切分为测试集和训练集，比例0.3\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)\n",
    "\n",
    "# 将训练集切分为两部分，一部分用于训练GBDT模型，另一部分输入到训练好的GBDT模型生成GBDT特征，\n",
    "#然后作为LR的特征。这样分成两部分是为了防止过拟合。\n",
    "X_train, X_train_lr, y_train, y_train_lr = train_test_split(X_train, y_train, test_size=0.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "#开始GBDT的训练\n",
    "from sklearn.preprocessing import OneHotEncoder \n",
    "from sklearn.linear_model import LogisticRegression\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#调用GBDT模型 \n",
    "gbdt = GradientBoostingClassifier(learning_rate=0.1,n_estimators=170,max_depth=9)\n",
    "grd_enc = OneHotEncoder()\n",
    "grd_lr = LogisticRegression(C=0.1,penalty='l1',solver='saga')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "GradientBoostingClassifier(criterion='friedman_mse', init=None,\n",
       "              learning_rate=0.1, loss='deviance', max_depth=9,\n",
       "              max_features=None, max_leaf_nodes=None,\n",
       "              min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "              min_samples_leaf=1, min_samples_split=2,\n",
       "              min_weight_fraction_leaf=0.0, n_estimators=170,\n",
       "              presort='auto', random_state=None, subsample=1.0, verbose=0,\n",
       "              warm_start=False)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''使用X_train训练GBDT模型，后面用此模型构造特征'''\n",
    "gbdt.fit(X_train, y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "gbdt auc: 0.69455\n"
     ]
    }
   ],
   "source": [
    "# 预测及AUC评测\n",
    "y_pred_gbdt = gbdt.predict_proba(X_test)[:, 1]\n",
    "gbdt_auc = metrics.roc_auc_score(y_test, y_pred_gbdt)\n",
    "print 'gbdt auc: %.5f' % gbdt_auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LogisticRegression(C=0.1, class_weight=None, dual=False, fit_intercept=True,\n",
       "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
       "          penalty='l1', random_state=None, solver='saga', tol=0.0001,\n",
       "          verbose=0, warm_start=False)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grd_lr.fit(X_train,y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "基于原有特征的LR AUC: 0.49393\n"
     ]
    }
   ],
   "source": [
    "y_pred_test = grd_lr.predict_proba(X_test)[:, 1]\n",
    "lr_test_auc = metrics.roc_auc_score(y_test, y_pred_test)\n",
    "print '基于原有特征的LR AUC: %.5f' % lr_test_auc\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "OneHotEncoder(categorical_features='all', dtype=<type 'numpy.float64'>,\n",
       "       handle_unknown='error', n_values='auto', sparse=True)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# fit one-hot编码器\n",
    "grd_enc.fit(gbdt.apply(X_train)[:, :, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' \n",
    "使用训练好的GBDT模型构建特征，然后将特征经过one-hot编码作为新的特征输入到LR模型训练。\n",
    "'''\n",
    "grd_lr.fit(grd_enc.transform(gbdt.apply(X_train_lr)[:, :, 0]), y_train_lr)\n",
    "# 用训练好的LR模型多X_test做预测\n",
    "y_pred_grd_lm = grd_lr.predict_proba(grd_enc.transform(gbdt.apply(X_test)[:, :, 0]))[:, 1]\n",
    "# 根据预测结果输出\n",
    "fpr_grd_lm, tpr_grd_lm, _ = metrics.roc_curve(y_test, y_pred_grd_lm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "基于原有特征的LR AUC: 0.71849\n"
     ]
    }
   ],
   "source": [
    "auc = metrics.roc_auc_score(y_test, y_pred_grd_lm)\n",
    "print '基于原有特征的LR AUC: %.5f' % auc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.398352435383\n"
     ]
    }
   ],
   "source": [
    "log = metrics.log_loss(y_test, y_pred_grd_lm)\n",
    "print log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
